define(["jquery","bbcare","fu/app/comm/comm"],function($,$bb,$comm){
	var servers = {};
	var URL = $bb.baseUrl("followup");

	/**
	 * 根据手机号码或姓名搜索
	 */
	servers.ajaxSearch = function(params,callback){
        $bb.ajax({
            url: URL + "followup/service/patient/patientQueueListQuery",
            data: JSON.stringify({
                data: params
            }),
            success: function (data) {
                if(data.success){
                    callback(data.data);
                }else{
                	$bb.toastShow(data.msg ? data.msg : "根据手机号码或姓名搜索出错！");
                }
            }
        })
    };

    /**
	 * 获取当前选中的患者的基本资料
	 */
	servers.getPatientInfo = function(list,patientId,examinationPaperName){
		for (var i = 0; i < list.length; i++) {
			if( list[i].patientId == patientId ){
                list[i].userId = list[i].patientId;
                list[i].type = examinationPaperName;
				return list[i];
			}
		}
	}

    /**
     * 获取胎龄
     */
    servers.getDiff = function(birthday,date,pregnancDay){
        var pregnancDay = parseInt(pregnancDay);
        var days;
        if( pregnancDay < 40*7 ){               // 小于40周：
            if( birthday ){                     // 出生：   days = 孕周 + 出生周龄
                days = $comm.dateDiff(birthday,date).days + pregnancDay;
            }else{                              // 未出生： days = 孕周;
                days = pregnancDay;
            }
        }else{                                  // 大于40周；
            if( birthday ){                     // 出生：   days = 出生周龄 + 40*7;
                days = $comm.dateDiff(birthday,date).days + 40*7;
            }else{                              // 未出生： days = ;  //不存在此情况：禁用：
                days = '';
            }
        }
        return days;
    };

    /**
     * 总状态
     */
    var totalStatus = function(){
    	return [
			{'item':'N',"status":1,"itemColor":"#AACA57","stage":"扭动阶段正常GMs"},
			{'item':'PR',"status":1,"itemColor":"#FACE64","stage":"单调性GMs"},
			{'item':'CS',"status":1,"itemColor":"#EF7255","stage":"痉挛-同步性GMs"},
			{'item':'Ch',"status":1,"itemColor":"#ED9772","stage":"混乱性GMs"},
			{'item':'F',"status":1,"itemColor":"#70C6C9","stage":"不安运动阶段的正常GMs"},
			{'item':'F-',"status":1,"itemColor":"#3B6FB3","stage":"不安运动缺乏"},
			{'item':'AF',"status":1,"itemColor":"#8B549B","stage":"异常性不安运动"},
		];
    }

    /* 
    * 根据年龄控制
    */
    var resultStatusFun = function(days){
    	var resultStatus = totalStatus();
        // 判断按钮的禁用状态：
        for( var i = 0; i < resultStatus.length; i++ ){
            if( days <= (40*7+ 6*7) && days >= (26*7) ) {
                if(resultStatus[i].item == 'F-' || resultStatus[i].item == 'AF' ){
                    resultStatus[i].status = 0;
                }
            }else if( days > (40*7 + 8*7) && days < (40*7 + 22*7) ){
                if(resultStatus[i].item == 'Ch' || resultStatus[i].item == 'PR' ){
                    resultStatus[i].status = 0;
                }
            }else{
                resultStatus[i].status = 0;
            }
        }
        return resultStatus;
    }

    /**
     * 处理评估结果
     */
    servers.handleResult = function(info){
        if( info.testTime ){
            var days = servers.getDiff(info.birthday,info.testTime,info.pregnancDay);
        }else{
            var nowDate = $comm.getCurTime().date;
            var days = servers.getDiff(info.birthday,nowDate,info.pregnancDay);
        }
        var resultStatus = resultStatusFun( days );
        return {
            "resultStatus":resultStatus,
        }
    }

	/**
	 * 处理患者基本信息
	 */
	servers.handleInfo = function(info){
		var nowDate = $comm.getCurTime().date;
		if( info.birthday ){
			var days = $comm.dateDiff(info.birthday,nowDate).days;
			info.weekAge = parseInt(days / 7) + "周" + days % 7 + "天";
		}else{
			info.weekAge = "未知";
		}
 		return {
 			"info":info,
            "nowDate":nowDate
 		};
	}

	/**
	 * gms保存评估结果
	 */
	servers.ajaxGetScore = function(params,callback,info){
		$bb.ajax({
            url: URL + "followup/service/patient/evaluation/getScore",
            data: JSON.stringify({
                data: {
                    "examinationPaperName": params.type,
                    "userId": params.userId,
                    "testTime": params.ps.testTime,
                    "postionIds": params,
                    "postionScores": params.ps
                }
            }),
            success: function (data) {
                if(data.success){
                    callback(data.data,info);
                }else{
                	$bb.toastShow(data.msg ? data.msg : "gms保存评估结果出错！");
                }
            }
        });
	}

	return servers;
});